﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using NavigationModule.Controllers;
using Infrastucture.Navigation;

namespace NavigationModule.Views
{
    /// <summary>
    /// Interaction logic for TopNavigationView.xaml
    /// </summary>
    public partial class TopNavigationView : UserControl, INavigationAware, INavigateAsync, IGlobalNavigate
    {
        private readonly LeftSideNavigationController _leftSideController;

        public TopNavigationView()
        {
            InitializeComponent();

            _leftSideController = new LeftSideNavigationController();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
            var service = new Services.TopNavigationService();
            this.DataContext = service.Get();
        }

        public void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback, NavigationParameters navigationParameters)
        {
            throw new NotImplementedException();
        }

        public void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback)
        {
            throw new NotImplementedException();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // todo: navigate
            TabControl t = sender as TabControl;

            if (t.SelectedItem != null)
            {
                try
                {
                    var link = t.SelectedItem as NavigationLink;
                    _leftSideController.Navigate(link);
                }
                catch (Exception ex)
                {
                    // keep going
                }
            }
        }

        public void NavigateByLink(NavigationLink link)
        {
            // TODO: need to programatically select tab
            this.globalLinks.IsEnabled = false;

           if (link.NavigationMenu == Infrastucture.NavigationMenu.Main)
           {
               this.globalLinks.SelectedItem = this.globalLinks.Items[0];
           }
           else if (link.NavigationMenu == Infrastucture.NavigationMenu.Projects)
           {
               this.globalLinks.SelectedItem = this.globalLinks.Items[1];
           }
           else if (link.NavigationMenu == Infrastucture.NavigationMenu.Tasks)
           {
               this.globalLinks.SelectedItem = this.globalLinks.Items[2];
           }
           else if (link.NavigationMenu == Infrastucture.NavigationMenu.Reports)
           {
               this.globalLinks.SelectedItem = this.globalLinks.Items[3];
           }
           else if (link.NavigationMenu == Infrastucture.NavigationMenu.Settings)
           {
               this.globalLinks.SelectedItem = this.globalLinks.Items[4];
           }

           this.globalLinks.IsEnabled = true;
        }
    }
}
